package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.SmsCouponParam;
import com.macro.mall.mapper.SmsParkingCouponMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.SmsParkingCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 优惠券管理Service实现类
 * Created by macro on 2018/8/28.
 */
@Service
public class SmsParkingCouponServiceImpl implements SmsParkingCouponService {

    @Autowired
    private SmsParkingCouponMapper couponMapper;

    @Override
    public int create(SmsParkingCoupon couponParam) {
        couponParam.setCount(couponParam.getPublishCount());
        couponParam.setUseCount(0);
        couponParam.setReceiveCount(0);
        //插入优惠券表
        int count = couponMapper.insert(couponParam);
        return count;
    }

    @Override
    public int delete(Long id) {
        //删除优惠券
        int count = couponMapper.deleteByPrimaryKey(id);
        return count;
    }


    @Override
    public int update(Long id, SmsParkingCoupon couponParam) {
        couponParam.setId(id);
        int count =couponMapper.updateByPrimaryKey(couponParam);
        return count;
    }

    @Override
    public List<SmsParkingCoupon> list(String name, Integer type, Integer pageSize, Integer pageNum) {
        SmsParkingCouponExample example = new SmsParkingCouponExample();
        SmsParkingCouponExample.Criteria criteria = example.createCriteria();
        if(!StrUtil.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(type!=null){
            criteria.andTypeEqualTo(type);
        }
        PageHelper.startPage(pageNum,pageSize);
        return couponMapper.selectByExample(example);
    }

    @Override
    public SmsParkingCoupon getItem(Long id) {
        return couponMapper.selectByPrimaryKey(id);
    }
}
